cc.Class({
    extends: cc.Component,

    properties: {
        tilePrefab : cc.Prefab,
        arrowPrefab : cc.Prefab,
        arrow : {
            default : null,
            visible : false
        }
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        this.arrow = cc.instantiate(this.arrowPrefab);
        this.arrow.zIndex = 999;
        this.arrow.active = false;
        this.node.addChild(this.arrow);

        this.discardArr = [];

        this._curIndex = 1;
        this._curRow = 1;
        this._limit = 8;
        this.marginX = -7;
        this.marginY = -10;
        this.offsetX = -3;
        this.offsetY = 0;
        this.step = 1;
    },

    createTile : function (MJId, startPos) {
        var tile = cc.instantiate(this.tilePrefab);
        var endPos = cc.p(- (this._curRow - 1) * (this.tilePrefab.data.width + this.marginX) + this._curIndex * this.offsetX, - (this._curIndex - 1) * (this.tilePrefab.data.height + this.marginY) + (this.tilePrefab.data.height + this.marginY) * (this.step / 2) * (this._curRow - 1) + this._curIndex * this.offsetY);

        tile.position = this.node.convertToNodeSpace(startPos);
        tile.zIndex = -this._curRow;
        
        if(!this.arrow.active){
            this.arrow.position = endPos.add(cc.p(this.tilePrefab.data.width / 2, 0));
            this.arrow.active = true;
        }

        this.discardArr.push(tile);
        this.node.addChild(tile);
        tile.getComponent("LeftDiscard").init(MJId);//调用脚本要放在addChild之后，因为addChild会触发脚本的onLoad方法
        tile.runAction(cc.bezierTo(0.3, [tile.position, tile.position.add(cc.p(100, 0)), endPos]));

        this._curIndex++;
        if(this._curIndex > this._limit){
            this._curIndex = 1;
            this._limit += this.step;
            this._curRow++;
        }
    },

    discard : function (MJIds, startPos) {
        for(var i in MJIds){
            this.createTile(MJIds[i], startPos[i]);
        }
    },

    delTile : function (MJId) {
        var tiles = this.discardArr;
        for(var i = this.discardArr.length - 1; i >= 0; i--){
            var tile = tiles[i];

            if(tile.getComponent("LeftDiscard").MJId != MJId){
                var prevTile = tiles[i - 1];
                if(prevTile){
                    tile.position = prevTile.position;
                    tile.zIndex = prevTile.zIndex;
                }
            }else{
                tile.destroy();
                this.discardArr.splice(i, 1);
                break;
            }
        }
    },
    
    end : function () {
        this.arrow.active = false;
    }
});
